import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class Command {
	private static final String TYPE_ADD = "ADD";
	private static final String TYPE_REMOVE = "REMOVE";
	private static final String TYPE_MODIFY = "MODIFY";
	private static final String TYPE_SEARCH = "SEARCH";
	private static final String TYPE_SORT = "SORT";
	private static final String TYPE_CLEAR ="CLEAR";
	private static final String TYPE_DISPLAY = "DISPLAY";
	private static final String TYPE_FINISH = "FINISH";
	private static final int INDEX_TYPE = 0;
	private static final int INDEX_TITLE_FOR_ADD = 1;
	private static final int INDEX_TIME_FOR_ADD = 2;
	private static final int INDEX_TAG_FOR_ADD = 3;
	private static final int INDEX_LEVEL_FOR_ADD = 4;
	
	private String type;
	private ArrayList<String> contents;

	public Command(String type, ArrayList<String> contents) {
		this.type = type;
		this.contents = contents;
	}
	public Command(ArrayList<String> contents) {
		this.type = contents.get(INDEX_TYPE);
		this.contents = contents;
	}
	public String getType() {
		return this.type ;
	}
	public String getTitleForAdd() {
		return this.contents.get(INDEX_TITLE_FOR_ADD);
	}
	public String getTypeFromContent() {
		return this.contents.get(INDEX_TYPE);
	}
	public String getTimeForAdd() {
		return this.contents.get(INDEX_TIME_FOR_ADD);
	}
	public String getTag() {
		return this.contents.get(INDEX_TAG_FOR_ADD);
	}
	public String getUrgencyLevel() {
		return this. contents.get(INDEX_LEVEL_FOR_ADD);
	}
	public ArrayList<String> getContentForSearch() {
		return this.contents;
	}
	public boolean isThisCommandValid () {
		switch (this.getType()) {
		case TYPE_ADD : {
			return isThisAddCommandValid();
		}
		
		case TYPE_REMOVE : {
			return isThisRemoveCommandValid();
		}
		case TYPE_DISPLAY:
		case TYPE_CLEAR :
		case TYPE_SORT : {
			return true;
		}
		case TYPE_SEARCH : {
			return isThisSCommandValid();
		}
		case TYPE_MODIFY : {
			return isThisModifyCommandValid();
		}
		case TYPE_FINISH : {
			return isThisFinishCommandValid();
		}
		default :
		{
			return false;
		}
		}
		
	}
	
	private boolean isThisRemoveCommandValid() {
		return true;
	}
	private boolean isThisAddCommandValid() {
		if(!this.getType().toUpperCase().equals(this.getTypeFromContent())) {
			System.out.println("the type is not match");
			return false;
		}
		if(this.getTitleForAdd()=="") {
			System.out.println("the title is null");
			return false;
		}
		if(this.getTimeForAdd()=="") {
			System.out.println("no time");
			return false;
		}
		Calendar today = Calendar.getInstance();
		try {
		Date theDate = new SimpleDateFormat("MMMM dd yyyy HH:mm",Locale.ENGLISH).parse(this.getTimeForAdd());
		Date currentDate = today.getTime();
		if(currentDate.after(theDate)) {
			return false;
		}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	private boolean isThisSCommandValid() {
		if(!this.getType().equals(this.getTypeFromContent())) {
			return false;
		}
		if(this.getContentForSearch().isEmpty()) {
			return false;
		}
		return true;
	}
	private boolean isThisModifyCommandValid() {
		if(!this.getType().equals(this.getTypeFromContent())) {
			return false;
		}
		if(this.getContentForSearch().isEmpty()) {
			return false;
		}
		if(this.getContentForSearch().size()<5) {
			return false;
		}
		return true;
	}
	private boolean isThisFinishCommandValid() {
		return this.getContentForSearch().size()<3;
	}
}
